<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class berita_acara extends My_Controller
{
	
	function __construct()
	{
		parent::__construct();
		  
		$this->load->model('mdl_master_parawaba', 'master_parawaba');		$this->load->model('mdl_berita_acara', 'berita_acara');
		
	}
	
	function index()
	{
		$data['can_view'] 	= $this->can_view();
		$data['can_insert'] = $this->can_insert();
		$data['can_update'] = $this->can_update();
		$data['can_delete'] = $this->can_delete();
		
		$this->open();
		
		# config pagination
		$config['base_url'] = base_url().'index.php/master_parawaba/index/';
		$config['total_rows'] = $this->db->count_all('master_parawaba');
		$config['per_page'] = '10';		$config['num_links'] = '3';		$config['uri_segment'] = '3';				$config['full_tag_open'] = '';		$config['full_tag_close'] = '';				$config['num_tag_open'] = '<div id="page-info">';		$config['num_tag_close'] = '</div>';				$config['cur_tag_open'] = '<li class="page-current"><strong>';		$config['cur_tag_close'] = '</strong></li>';				$config['prev_link'] = '<li class="page-left"></li>';		$config['prev_tag_open'] = '';		$config['prev_tag_close'] = '';				$config['next_link'] = '<li class="page-right"></li>';		$config['next_tag_open'] = '';		$config['next_tag_close'] = '';				$config['last_link'] = '<li class="page-far-right"></li>';		$config['last_tag_open'] = '';		$config['last_tag_close'] = '';				$config['first_link'] = '<li class="page-far-left"></li>';		$config['first_tag_open'] = '';		$config['first_tag_close'] = '';
		
		$this->pagination->initialize($config);	
		# end config pagination
		
		# get data
		$data['results'] = $this->master_parawaba->getItem($config['per_page'], $this->uri->segment(3));
		
		# load view
		$this->load->view('master_parawaba/master_parawaba_list', $data);
		
		$this->close();
	}	
	function insert()	{ 		$this->open();		if($_POST)		{			$bulan				= date("m");			$data['tanggal_awal'] 	= $this->input->post('tanggal_awal');;			$data['tanggal_akhir'] 	= $this->input->post('tanggal_akhir');;			$data['tahun'] 		= $this->input->post('tahun');			$data['parawaba'] 	= $this->input->post('parawaba');			$data['nilai'] 		= $this->input->post('nilai');			$data['keterangan'] = $this->input->post('keterangan');			/*			echo $data['tanggal'] ;echo"<br/>";			echo $data['tahun'] ;echo"<br/>";			echo $data['parawaba'] ;echo"<br/>";			echo $data['nilai'] ;echo"<br/>";			echo $data['keterangan'] ;			*/			$jumlah_sudah_bayar = $this->berita_acara->getSudahBayar($bulan,$data['tahun'], $data['parawaba']);			$data['sudah_bayar']= $jumlah_sudah_bayar[0]['sudah_bayar'];			$data['sisa']		= $data['nilai'] - $data['sudah_bayar'];			$this->berita_acara->insert($data);			echo "<script LANGUAGE='javascript'>			$.fancybox.close();</script>";			redirect('piutang/index');							/*			echo "<script LANGUAGE='javascript'>parent.window.opener.location='".base_url()."index.php/piutang/index';					window.close();</script>";			*/		}		else		{			$data['results'] = $this->master_parawaba->getItemAll(); 			$this->load->view('berita_acara/berita_acara_add', $data); 		}  		# get data		$this->close(); 	}	function cek_berita()	{		$kirim = $this->input->post('kirim');		$pieces = explode("#", $kirim);		$tahun = $pieces[0];		$parawaba = $pieces[1];				$hasil_parawaba = $this->berita_acara->cek($tahun,$parawaba);        if(count($hasil_parawaba) > 0)		{             echo '1';        }else		{            echo '2';        }	}
	
	function insert2()
	{
		if ($this->can_insert() == FALSE){
			redirect('auth/failed');
		}
		
		$this->open();
		# Data
		$data['id'] = $this->input->post('id');
		$data['nama'] = $this->input->post('nama');
		$data['kota'] = $this->input->post('kota');
		$data['status_bhp'] = $this->input->post('status_bhp');
		$data['status_ba_uso'] = $this->input->post('status_ba_uso');		$data['status_bayar_uso'] = $this->input->post('status_bayar_uso');
		$data['userid'] = get_userid();
		
		
		# set rules validation
		$this->form_validation->set_rules('kode_master_parawaba', 'Kode master_parawaba', 'required');
		$this->form_validation->set_rules('nama', 'Nama', 'required');
		$this->form_validation->set_rules('kota', 'kota', 'trim');
		$this->form_validation->set_rules('telpon', 'Telpon', 'trim');
		$this->form_validation->set_rules('saldo_hutang', 'saldo_hutang', 'trim|numeric');
		
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
		
		# set message validation
		$this->form_validation->set_message('required', 'Field %s harus diisi!');
		$this->form_validation->set_message('alpha', 'Field %s harus diisi hanya dengan huruf!');
		$this->form_validation->set_message('numeric', 'Field %s harus diisi hanya dengan angka!');
		
		
		if ($this->form_validation->run() == FALSE){
			
			$this->load->view('master_parawaba/master_parawaba_add',$data);
			
		}else{	
			$this->master_parawaba->insert($data);
			$this->session->set_flashdata('message', 'Data master_parawaba Berhasil disimpan.');
			redirect('master_parawaba');
		}
		
		$this->close();
	}
	
	function update($id)
	{
		if ($this->can_update() == FALSE){
			redirect('auth/failed');
		}
		
		$this->open();
		
		$data['result'] 		= $this->master_parawaba->getItemById($id);
		
		$data['id'] = $id;
		$data['nama'] = $data['result']->row()->nama;
		$data['kota'] = $data['result']->row()->kota;
		$data['status_bhp'] = $data['result']->row()->status_bhp;		$data['status_ba_uso'] = $data['result']->row()->status_ba_uso;
		$data['status_bayar_uso'] = $data['result']->row()->status_bayar_uso;
		
		
		$this->load->view('master_parawaba/master_parawaba_edit', $data);
		
		$this->close();
	}
	
	function process_update()
	{
		if ($this->can_update() == FALSE){
			redirect('auth/failed');
		}
		
		$this->open();
		
		# Data
		$data['id_master_parawaba'] = $this->input->post('id_master_parawaba');
		$data['kode_master_parawaba'] = $this->input->post('kode_master_parawaba');
		$data['nama'] = $this->input->post('nama');
		$data['alamat'] = $this->input->post('alamat');
		$data['telpon'] = $this->input->post('telpon');
		$data['saldo_hutang'] = $this->input->post('saldo_hutang');
		$data['userid'] = get_userid();
		
		
		# set rules validation
		$this->form_validation->set_rules('kode_master_parawaba', 'Kode master_parawaba', 'required');
		$this->form_validation->set_rules('nama', 'Nama', 'required');
		$this->form_validation->set_rules('alamat', 'Alamat', 'trim');
		$this->form_validation->set_rules('telpon', 'Telpon', 'trim');
		$this->form_validation->set_rules('saldo_hutang', 'saldo_hutang', 'trim|numeric');
		
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
		
		# set message validation
		$this->form_validation->set_message('required', 'Field %s harus diisi!');
		$this->form_validation->set_message('alpha', 'Field %s harus diisi hanya dengan huruf!');
		$this->form_validation->set_message('numeric', 'Field %s harus diisi hanya dengan angka!');
		
		
		if ($this->form_validation->run() == FALSE){
			$this->load->view('master_parawaba/master_parawaba_edit',$data);
		}else{
			$this->master_parawaba->update($data['id_master_parawaba'], $data);
			$this->session->set_flashdata('message', 'Data master_parawaba Berhasil diupdate.');
			redirect('master_parawaba');
		}
		
		$this->close();
		
	}
	
	function delete($id)
	{
		if ($this->can_delete() == FALSE){
			redirect('auth/failed');
		}
		
		$this->master_parawaba->delete($id);
		$this->session->set_flashdata('message', 'Data master_parawaba Berhasil dihapus.');
		redirect('master_parawaba');
	}
	
}